package com.lagou.QuestionsSystem;

import com.lagou.client.ClientInitClose;
import com.lagou.client.ClientScanner;
import com.lagou.model.Question;
import com.lagou.model.User;

import java.io.IOException;
import java.util.ArrayList;

public class StudentsExamSystem {

    /**
     * 合成复用原则
     */
    private ClientInitClose cic;

    /**
     * 通过构造方法实现成员变量的初始化
     */
    public StudentsExamSystem(ClientInitClose cic){
        this.cic = cic;
    }

    //绘制考试系统界面
    public void examSystemView() throws IOException, ClassNotFoundException {

        while (true) {
            System.out.println("   \n\n          考试系统");
            System.out.println("--------------------------------");
            System.out.print("   [1] 开始考试");
            System.out.println("   [2] 查询成绩");
            System.out.println("   [0] 退出");
            System.out.println("--------------------------------");
            System.out.println("请选择要进行的业务编号");
            int choose = ClientScanner.getScanner().nextInt();
            switch (choose) {
                case 1:
                    cic.getPs().println("beginExam");
                    beginExam();
                    break;
                case 2:
                    cic.getPs().println("showScore");
                    showScore();
                    break;
                case 0:
                    cic.getPs().println("exit");
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("输入错误，请重新选择");
            }
        }
    }


    public void beginExam() throws IOException, ClassNotFoundException {
        //读取服务器发送的试题
        ArrayList list = (ArrayList) cic.getOis().readObject();
        //转换成泛型为Question类的集合
        ArrayList<Question> exam = new ArrayList<>();
        exam.addAll(list);
        System.out.println("接收到试题，开始考试");
        //开始答题
        //打印题目和选项
        for(int i  = 0; i < exam.size(); ++i){
            //打印题目
            System.out.println("\n" + (i+1) + "." + exam.get(i).getTitle());
            //打印选项
            for(int j = 0; j < exam.get(i).getOption().length; ++j){
                System.out.print(exam.get(i).getOption()[j] + "     ");
            }
            System.out.println("");
            System.out.println("请输入答案选项");
            cic.getPs().println(ClientScanner.getScanner().next());
        }
        System.out.println("答题完毕，祝你好运");
    }


    public void showScore() throws IOException, ClassNotFoundException {
        //接收分数信息
        ArrayList score = (ArrayList) cic.getOis().readObject();
        //转换成泛型为Integer类的集合
        //ArrayList<Integer> score = new ArrayList<>();
        //score.addAll(list);
        //打印分数信息
        for(int i  = 0; i < score.size(); ++i){
            System.out.println("第" + (i + 1) + "次考试的成绩为 " + score.get(i) + " 分");
        }
    }
}
